<?php
class Article_show
{
	private static $article_cachetime = 86400;
	private static $tags = 25;//tag云显示的tag数量
	private static $tag_cachetime = 300;
	private static $article_rss_cachetime = 3600;
	public static function show_list($article_id,$updtime)
	{
		$cache_key = "article_show_list_{$article_id}_{$updtime}";
		$return = Yii::app()->cache->get($cache_key);
		if($return === false)
		{
			$article = Article::model()->with('tag_relation')->findByPk($article_id);
			if(!$article)
			return '';
			$article_url = Yii::app()->params['domain'].'/article/'.$article->url_key.'.html';
			$return .= CHtml::openTag('div',array('id'=>'item-'.$article->id,'class'=>'item'));
			//date div
			$return .= CHtml::openTag('div',array('class'=>'date'));
			$return .= CHtml::tag('div',array('class'=>'month'),date('M',strtotime($article->create_time)));
			$return .= CHtml::tag('div',array('class'=>'day'),date('d',strtotime($article->create_time)));
			$return .= CHtml::closeTag('div');
			//title
			$return .= CHtml::openTag('h2',array('class'=>'title'));
			$return .= CHtml::tag('a',array('title'=>$article->title,'href'=>$article_url),$article->title);
			$return .= CHtml::closeTag('h2');
			//content
			$return .= CHtml::tag('div',array('class'=>'content'),wpautop($article->content));
			//tag links
			$return .= CHtml::openTag('div',array('class'=>'cat_tags clear'));
			$return .= CHtml::openTag('span',array('class'=>'tags'));
			$return .= '标签: ';
			foreach ($article->tag_relation as $k=>$v)
			{
				if($k > 0)
				{
					$return .= ', ';
				}
				$return .= CHtml::tag('a',array('rel'=>'tag','href'=>'/tag/'.urlencode($v->tag->tag)),$v->tag->tag);
			}
			$return .= CHtml::closeTag('span');
			$return .= CHtml::closeTag('div');
			//$return .= '<div id="jiathis_style_32x32" onmouseover="setShare(\''.addslashes($article->title).'\',\''.$article_url.'\',\''.addslashes(mb_substr(trim(strip_tags($article->content)),0,80,'UTF-8')).'…\')"><a class="jiathis_button_twitter"></a><a class="jiathis_button_tsina"></a><a class="jiathis_button_googleplus"></a><a class="jiathis_button_kaixin001"></a><a class="jiathis_button_fanfou"></a><a class="jiathis_button_tqq"></a><a class="jiathis_button_douban"></a><a class="jiathis_button_t163"></a><a class="jiathis_button_fb"></a><a class="jiathis_button_linkedin"></a><a class="jiathis_button_mop"></a><a class="jiathis_button_tsohu"></a><a class="jiathis_button_digg"></a><a href="http://www.jiathis.com/share?uid=1601482" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a><a class="jiathis_counter_style"></a></div>';
			$return .= CHtml::closeTag('div');
			Yii::app()->cache->set($cache_key, $return,self::$article_cachetime);
		}
		return $return;
	}

	public static function show_content($article_id,$updtime)
	{
		$cache_key = "article_show_content_{$article_id}_{$updtime}";
		$return = Yii::app()->cache->get($cache_key);
		if($return === false)
		{
			$article = Article::model()->with('tag_relation')->findByPk($article_id);
			if(!$article)
			return '';
			$article_url = Yii::app()->params['domain'].'/article/'.$article->url_key.'.html';
			$return .= CHtml::openTag('div',array('id'=>'item-'.$article->id,'class'=>'item'));
			//date div
			$return .= CHtml::openTag('div',array('class'=>'date'));
			$return .= CHtml::tag('div',array('class'=>'month'),date('M',strtotime($article->create_time)));
			$return .= CHtml::tag('div',array('class'=>'day'),date('d',strtotime($article->create_time)));
			$return .= CHtml::closeTag('div');
			//title
			$return .= CHtml::openTag('h2',array('class'=>'title'));
			$return .= CHtml::tag('a',array('title'=>$article->title,'href'=>$article_url),$article->title);
			$return .= CHtml::closeTag('h2');
			//content
			$return .= CHtml::tag('div',array('class'=>'content'),wpautop($article->content));
			//tag links
			$return .= CHtml::openTag('div',array('class'=>'cat_tags clear'));
			$return .= CHtml::openTag('span',array('class'=>'tags'));
			$return .= '标签: ';
			foreach ($article->tag_relation as $k=>$v)
			{
				if($k > 0)
				{
					$return .= ', ';
				}
				$return .= CHtml::tag('a',array('rel'=>'tag','href'=>'/tag/'.urlencode($v->tag->tag)),$v->tag->tag);
			}
			$return .= CHtml::closeTag('span');
			$return .= '<div class="weibo_share"><wb:share-button appkey="4N3bMs" addition="number" type="button" ralateUid="1644791675"></wb:share-button></div>';//weibo微博分享
			$return .= CHtml::closeTag('div');
			//$return .= '<div id="jiathis_style_32x32" onmouseover="setShare(\''.addslashes($article->title).'\',\''.$article_url.'\',\''.addslashes(mb_substr(trim(strip_tags($article->content)),0,80,'UTF-8')).'…\')"><a class="jiathis_button_twitter"></a><a class="jiathis_button_tsina"></a><a class="jiathis_button_googleplus"></a><a class="jiathis_button_kaixin001"></a><a class="jiathis_button_fanfou"></a><a class="jiathis_button_tqq"></a><a class="jiathis_button_douban"></a><a class="jiathis_button_t163"></a><a class="jiathis_button_fb"></a><a class="jiathis_button_linkedin"></a><a class="jiathis_button_mop"></a><a class="jiathis_button_tsohu"></a><a class="jiathis_button_digg"></a><a href="http://www.jiathis.com/share?uid=1601482" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a><a class="jiathis_counter_style"></a></div>';
			$return .= CHtml::closeTag('div');
			Yii::app()->cache->set($cache_key, $return,self::$article_cachetime);
		}
		return $return;
	}
	
	public static function tagcloud()
	{
		$return = Yii::app()->cache->get('global_tagcloud');
		if($return === false)
		{
			$return = '';
			$tags = Tag::model()->findAll('1 order by used_times desc limit 100');
			$rand_keys = array_rand($tags,self::$tags);
			foreach ($rand_keys as $k)
			{
				$t = $tags[$k];
				$fontsize = rand(12,22);
				$return .= CHtml::tag('a',array('title'=>$t->tag,'href'=>'/tag/'.urlencode($t->tag),'style'=>'font-size:'.$fontsize.'px'),$t->tag);
			}
			Yii::app()->cache->set('global_tagcloud', $return,self::$tag_cachetime);
		}
		return $return;
	}
	
	public static function feed($article_id,$updtime)
	{
		$cahce_key = "article_cache_rss_{$article_id}_{$updtime}";
		$return = Yii::app()->cache->get($cahce_key);
		if(!$return)
		{
			$article = Article::model()->findByPk($article_id);
			$return = '';
			$return .= CHtml::openTag('item').PHP_EOL;
			$return .= CHtml::tag('title',array(),$article->title).PHP_EOL;
			$return .= CHtml::tag('link',array(),Yii::app()->params['domain'].'/article/'.$article->url_key.'.html').PHP_EOL;
			$return .= CHtml::tag('guid',array('isPermaLink'=>'false'),'ixuepin_article_'.$article->id).PHP_EOL;
			$return .= CHtml::tag('description',array(),'<![CDATA['.wpautop($article->content).']]>').PHP_EOL;
			$return .= CHtml::tag('pubDate',array(),date(DATE_RSS,strtotime($article->create_time))).PHP_EOL;
			$return .= CHtml::closeTag('item').PHP_EOL;
			Yii::app()->cache->set($cahce_key, $return,self::$article_rss_cachetime);
		}
		return $return;
	}
}

function _autop_newline_preservation_helper( $matches ) {
	return str_replace("\n", "<WPPreserveNewline />", $matches[0]);
}
/**
 * 
 * 来自Wordpress的格式化函数
 */
function wpautop($pee, $br = 1) {

	if ( trim($pee) === '' )
		return '';
	$pee = $pee . "\n"; // just to make things a little easier, pad the end
	$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
	// Space things out a little
	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
	if ( strpos($pee, '<object') !== false ) {
		$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
		$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
	}
	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
	// make paragraphs, including one at the end
	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
	$pee = '';
	foreach ( $pees as $tinkle )
		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
	$pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
	if ($br) {
		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
	}
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
	if (strpos($pee, '<pre') !== false)
		$pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );

	return $pee;
}